package com.example.myandroidtest.ui.custom;

import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int desiredSize = (int) (100 * getResources().getDisplayMetrics().density);
        int width = measureDimension(desiredSize, widthMode, widthSize);
        int height = measureDimension(desiredSize, heightMode, heightSize);

        setMeasuredDimension(width, height);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private int measureDimension(int desiredSize, int mode, int size) {
        int result;
        if (mode == MeasureSpec.EXACTLY) {
            result = size;
        } else if (mode == MeasureSpec.AT_MOST) {
            result = Math.min(desiredSize, size);
        } else {
            result = desiredSize;
        }
        return result;
    }

    // ACTION_DOWN  ACTION_MOVE  ACTION_UP  ACTION_CANCEL

    //ViewGroup 事件分发
    //View 事件处理
    //dispatchTouchEvent ->onTouchListener()->onTouchEvent()->onTouch()

    //Activity.dispatchTouchEvent()->Window.dispatchTouchEvent()->ViewGroup.dispatchTouchEvent()
    //


    ViewGroup viewGroup;
    View view;
    ImageView imageView;//
}
